<%= lines(autogen_notice(:go, pwd)) -%>

package google

import (
  "fmt"
  "testing"

  "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
  "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
<%

resource_name = product_ns + object.name
tf_product = (@config.legacy_name || product_ns).underscore
tf_resource = object.name.underscore
terraform_name = object.legacy_name || "google_#{tf_product}_#{tf_resource}"
-%>
<%
# @api.version_obj_or_closest(version) is slightly wrong; we want the _object_ version or the generation version.
# Ultimately this won't matter though, since the API default should always be less than the object default.
object.examples
  .reject(&:skip_test)
  .reject { |e| @api.version_obj_or_closest(version) < @api.version_obj_or_closest(e.min_version) }
  .each do |example|

	# {Compute}{Address}_{addressBasic}
    test_slug = "#{resource_name}_#{example.name.camelize(:lower)}Example"

    ignore_read = object.all_user_properties
      .select{|p| p.url_param_only || p.ignore_read || p.is_a?(Api::Type::ResourceRef) }
      .map { |p| p.name.underscore }
      .concat(example.ignore_read_extra)

    # Use explicit version for the example if given.
    # Otherwise, use object version.
    example_version = example.min_version || object.min_version.name
-%>

func TestAcc<%= test_slug -%>(t *testing.T) {
<% if example.skip_vcr -%>
	skipIfVcr(t)
<% end -%>
  t.Parallel()

	context := map[string]interface{} {
<%= lines(indent(compile(pwd + '/templates/terraform/env_var_context.go.erb'), 4)) -%>
	<% unless example.test_vars_overrides.nil? -%>
	<% example.test_vars_overrides.each do |var_name, override| -%>
			"<%= var_name %>": <%= override %>,
	<% end -%>
	<% end -%>
			"random_suffix": randString(t, 10),
	}

<% versioned_provider = !example_version.nil? && example_version != 'ga' -%>
	vcrTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		<% unless versioned_provider -%>
		Providers: testAccProviders,
		<% else -%>
		Providers: testAccProvidersOiCS,
		<% end -%>
		<% if example.pull_external -%>
		ExternalProviders: map[string]resource.ExternalProvider{
			"random": {},
		},
		<% end -%>
		<% unless object.skip_delete -%>
		CheckDestroy: testAccCheck<%= "#{resource_name}" -%>DestroyProducer(t),
		<% end -%>
		Steps: []resource.TestStep{
			{
				Config: testAcc<%= test_slug -%>(context),
			},
		<%#- The Terraform test runner doesn't let us import resources with aliased providers (yet) -%>
		<% unless example.skip_import_test || versioned_provider -%>
			{
				ResourceName:      "<%= terraform_name -%>.<%= example.primary_resource_id -%>",
				ImportState:       true,
				ImportStateVerify: true,
		<%- unless ignore_read.empty? -%>
				ImportStateVerifyIgnore: <%= go_literal(ignore_read) %>,
		<%- end -%>
			},
		<% end -%>
		},
	})
}

func testAcc<%= test_slug -%>(context map[string]interface{}) string {
<%= example.config_test(pwd) -%>
}
<%- end %>

<% unless object.skip_delete -%>
func testAccCheck<%= resource_name -%>DestroyProducer(t *testing.T) func(s *terraform.State) error {
	return func(s *terraform.State) error {
		for name, rs := range s.RootModule().Resources {
			if rs.Type != "<%= terraform_name -%>" {
				continue
			}
			if strings.HasPrefix(name, "data.") {
				continue
			}

		<% if object.custom_code.test_check_destroy -%>
	<%= lines(compile(pwd + '/' + object.custom_code.test_check_destroy)) -%>
		<% else -%>
		config := googleProviderConfig(t)

		url, err := replaceVarsForTest(config, rs, "<%= "{{#{object.__product.name}BasePath}}#{object.self_link_uri}" -%>")
		if err != nil {
			return err
		}

		billingProject := ""

		if config.BillingProject != "" {
			billingProject = config.BillingProject
		}

		_, err = sendRequest(config, "<%= object.read_verb.to_s.upcase -%>", billingProject, url, config.userAgent, nil<%= object.error_retry_predicates ? ", " + object.error_retry_predicates.join(',') : "" -%>)
		if err == nil {
				return fmt.Errorf("<%= resource_name -%> still exists at %s", url)
			}
		<% end -%>
		}

		return nil
	}
}
<% end -%>
